#include "Graphic.h"
#include "EngineSystem.h"



bool CALLBACK ModifyDeviceSettings( DXUTDeviceSettings* pDeviceSettings, void* pUserContext )
{
	return true;
}
bool CALLBACK IsD3D11DeviceAcceptable( const CD3D11EnumAdapterInfo *AdapterInfo, UINT Output, const CD3D11EnumDeviceInfo *DeviceInfo,
										   DXGI_FORMAT BackBufferFormat, bool bWindowed, void* pUserContext )
{
	return true;
}
HRESULT CALLBACK OnD3D11CreateDevice( ID3D11Device* pd3dDevice, const DXGI_SURFACE_DESC* pBackBufferSurfaceDesc,
										  void* pUserContext )
{
	return 0;
}
HRESULT CALLBACK OnD3D11ResizedSwapChain( ID3D11Device* pd3dDevice, IDXGISwapChain* pSwapChain,
											  const DXGI_SURFACE_DESC* pBackBufferSurfaceDesc, void* pUserContext )
{
	return 0;
}
void CALLBACK OnD3D11ReleasingSwapChain( void* pUserContext )
{

}
void CALLBACK OnD3D11DestroyDevice( void* pUserContext )
{
}
//void CALLBACK OnD3D11FrameRender( ID3D11Device* pd3dDevice, ID3D11DeviceContext* pd3dImmediateContext, double fTime,
//									  float fElapsedTime, void* pUserContext )
//{
//}



GraphicSystem::GraphicSystem()
{
}

GraphicSystem::~GraphicSystem()
{
}

HRESULT GraphicSystem::Initialize(int wnd_w,int wnd_h,WCHAR* appname)
{
	DXUTSetCallbackD3D11DeviceAcceptable( IsD3D11DeviceAcceptable );
    DXUTSetCallbackD3D11DeviceCreated( OnD3D11CreateDevice );
    DXUTSetCallbackD3D11SwapChainResized( OnD3D11ResizedSwapChain );
	DXUTSetCallbackFrameMove( OnFrameMove );
    DXUTSetCallbackD3D11FrameRender( OnD3D11FrameRender );
    DXUTSetCallbackD3D11SwapChainReleasing( OnD3D11ReleasingSwapChain );
    DXUTSetCallbackD3D11DeviceDestroyed( OnD3D11DestroyDevice );
	DXUTInit( true, true,NULL); // Parse the command line, show msgboxes on error, no extra command line params
    DXUTSetCursorSettings( true, true ); // Show the cursor and clip it when in full screen
	DXUTSetWindowWidth(1024);
	DXUTSetWindowHeight(768);
    DXUTCreateWindow(  appname  );
	DXUTCreateDevice( D3D_FEATURE_LEVEL_11_0, true, wnd_w, wnd_h);


	this->m_pFirstPersonCamera = new CFirstPersonCamera();
	D3DXVECTOR3                 DefaultEye           ( 30.0f, 150.0f, -150.0f );
	D3DXVECTOR3                 DefaultLookAt        ( 0.0f, 60.0f, 0.0f );
	D3DXVECTOR3                 Up                   ( 0.0f, 1.0f, 0.0f );
	D3DXVECTOR3                 Down                 = -Up;
	FLOAT                       NearPlane            = 2.0f;
	FLOAT                       FarPlane             = 4000.0f;
	FLOAT                       FOV                  = D3DX_PI / 4.0f;
	m_pFirstPersonCamera->SetViewParams(&DefaultEye,&DefaultLookAt);
	const DXGI_SURFACE_DESC* pBackBufferSurfaceDesc = DXUTGetDXGIBackBufferSurfaceDesc();
	float fAspectRatio =  pBackBufferSurfaceDesc->Width / ( FLOAT )pBackBufferSurfaceDesc->Height;
	if(pBackBufferSurfaceDesc)
	{
		m_pFirstPersonCamera->SetProjParams(FOV,fAspectRatio,NearPlane,FarPlane);
	}
	else
	{
		DXUTGetShowMsgBoxOnError();
		return 0;
	}
}

void GraphicSystem::MainRender(ID3D11Device* pd3dDevice, ID3D11DeviceContext* pd3dImmediateContext, double fTime,
									  float fElapsedTime)
{
	
}

HRESULT GraphicSystem::MultithreadRenderModeOn()
{
	return S_OK;
}

HRESULT GraphicSystem::MultithreadRenderModeOff()
{
	return S_OK;
}